package com.ruoyi.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.weixin.entity.Account;
import com.ruoyi.weixin.entity.AccountFam;
import com.ruoyi.weixin.entity.AccountTab;
import com.ruoyi.weixin.mapper.AccountFamMapper;
import com.ruoyi.weixin.mapper.AccountTabMapper;
import com.ruoyi.weixin.mapper.AccountsMapper;
import com.ruoyi.weixin.service.AccountFamService;
import com.ruoyi.weixin.vo.AccountPararm;
import com.ruoyi.weixin.vo.MouthVo;
import com.ruoyi.weixin.vo.Paihang;
import com.ruoyi.weixin.vo.Total;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (AccountFam)表服务实现类
 *
 * @author makejava
 * @since 2024-01-11 14:34:13
 */
@Service("accountFamService")
public class AccountFamServiceImpl extends ServiceImpl<AccountFamMapper, AccountFam> implements AccountFamService {
    @Autowired
    AccountFamMapper accountsMapper;

    @Autowired
    AccountTabMapper tabMapper;

    @Override
    @Transactional
    public Map<String, Object> accountList(AccountPararm accountPararm) throws ParseException {
        Page<Total> page = new Page<>(accountPararm.getPageNum(),accountPararm.getPageSize());
        HashMap<String, Object> map = new HashMap<>();
        MouthVo mouthVo=accountsMapper.getMounth(accountPararm);

        IPage<Total> totalIPage=accountsMapper.getDay(page,accountPararm);
        List<Total> totalList = totalIPage.getRecords();
        List<Account> accountList=null;
        for (Total total : totalList) {
            accountList=accountsMapper.getAccount(total.getDate(),accountPararm.getUserId());
            total.setAccountList(accountList);
        }
        totalIPage.setRecords(totalList);
        map.put("mouthVo",mouthVo);
        map.put("totalIPage",totalIPage);
        return map;
    }

    @Override
    public  Page<Account>  selectPaihang(Paihang paihang) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Account> page = new Page<>(paihang.getPageNum(),paihang.getPageSize());
        IPage<Account> parhangAccount=accountsMapper.selectPaihang(page,paihang);
        return page;
    }

    @Override
    public Map<String, Object> yearAcount(Paihang paihang) {
        HashMap<String, Object> map = new HashMap<>();
        float num=0l;
        float jian=0l;
        List<MouthVo> yearAcountAccount=accountsMapper.yearAcount(paihang);
        List<String> years=accountsMapper.getyearList(paihang.getUserId());
        for (MouthVo mouthVo : yearAcountAccount) {
            jian+=mouthVo.getJian();
            num+=mouthVo.getNum();
        }
        map.put("years",years);
        map.put("jian",jian);
        map.put("num",num);
        map.put("year",yearAcountAccount);
        return map;
    }

    @Override
    public List<AccountTab> accountTbList(String type) {
        List<AccountTab> accountTabs = tabMapper.selectList(new LambdaQueryWrapper<AccountTab>().eq(AccountTab::getType, type));
        return accountTabs;
    }

    @Override
    public int accountUpdate(String id) {
        return accountsMapper.deleteById(id);
    }
}
